var AIRCRAFT_SHIFT_SIZE = 2;
var AIRCRAFT_NO_SHIFT   = 0;
var AIRCRAFT_IMAGE_SRC  = "img/airplane.png";

/**
 * @param x
 * @param y
 * @param z
 * @returns { Aircraft }
 */
Aircraft = function( x, y, z )
{
     
    this.horizontalShift = AIRCRAFT_NO_SHIFT; // 0 to stop
    this.verticalShift   = AIRCRAFT_NO_SHIFT; // 0 to stop
    this.weapons = new Array;
    this.selectedWeapon = null;
    
    this.initImgComponent( AIRCRAFT_IMAGE_SRC, x, y, z );

    this.update = function( elapsedTime, context )
    {
        if ( this.horizontalShift )
        {
            this.x += this.horizontalShift; // 0 to stop, 1 to right, -1 to left
        }
        if ( this.verticalDirection )
        {
            this.y += this.verticalDirection; // 0 to stop, 1 to up, -1 to down
        }
    };

    this.draw = function( elapsedTime, context )
    {
        context.drawImage( this.image, this.x, this.y );
    };

    this.keyDown = function( event )
    {
        if ( event.keyCode == Key.RIGHT )
        {
            this.horizontalShift = AIRCRAFT_SHIFT_SIZE;    //right
        }
        if ( event.keyCode == Key.LEFT )
        {
            this.horizontalShift = -AIRCRAFT_SHIFT_SIZE;   //left
        }
        if ( event.keyCode == Key.UP )
        {
            this.verticalDirection = AIRCRAFT_SHIFT_SIZE;  //up
        }
        if ( event.keyCode == Key.DOWN )
        {
            this.verticalDirection = -AIRCRAFT_SHIFT_SIZE; // 1 to down
        }
        if ( event.keyCode == Key.SPACE && this.selectedWeapon )
        {
            this.selectedWeapon.pullTrigger();
        }        
        if ( event.keyCode == Key.R && this.selectedWeapon )
        {
            this.selectedWeapon.reload();
        }        
        if ( event.keyCode == Key.ONE )
        {
            this.addWeapon( new Weapon( this ) );
        }
        if ( event.keyCode == Key.TWO )
        {
            this.addWeapon( new FireWeapon( this ) );
        }
        if ( event.keyCode == Key.ZERO )
        {
            this.selectedWeapon = null;
        }
    };

    this.addWeapon = function( weapon )
    {
        if ( this.weapons[ weapon.name ] )
        {
            this.selectedWeapon = this.weapons[ weapon.name ];
        }
        else
        {
            this.weapons[ weapon.name ] = weapon;
            
            this.selectedWeapon = weapon;
            
            weapon = null;
        }
    };
    
    this.keyUp = function( event )
    {
        if ( this.horizontalShift && ( event.keyCode == Key.LEFT || event.keyCode == Key.RIGHT ) )
        {
            this.horizontalShift = AIRCRAFT_NO_SHIFT;   //stop
        }
        if ( this.verticalDirection && ( event.keyCode == Key.UP || event.keyCode == Key.DOWN ) )
        {
            this.verticalDirection = AIRCRAFT_NO_SHIFT; //stop
        }
    };

};

Aircraft.prototype = new ImgComponent;